---
title: 'Section 7: Alternative Explanations'
author: "Milan Svolik"
date: "4/20/2023"
output:
  pdf_document: default
  html_document:
    df_print: paged
---

```{r setup, include=FALSE}
rm(list = ls(all = TRUE))
library(tidyverse)
library(stargazer)
library(lmtest)
library(reshape2)
library(foreign)
library(ggplot2)
library(broom)
library(estimatr)

set.seed(5877)
```

# Table 7.5
* Economic indicators: Descriptive statistics
```{R}
df_econ <- read_csv("data/Economic Indicators, June 2018 to September 2019.csv") 
df_econ <-  df_econ %>% select(date, xrate=`exchange rate`, growth=`GDP change`, unemployment, inflation)
df_econ$growth <- 100*df_econ$growth
df_econ$inflation <- 100*df_econ$inflation 

stargazer(data.frame(df_econ)[,-1], type = "latex", title="Descriptive statistics", digits=2,
 covariate.labels=c("Exchange\nrate", "Growth\n ", "Unemployment\n", "Inflation\n"))
```

# Figure 7.28: Economic indicators: June 2018-September 2019
```{R}
df_plot <-  df_econ %>% filter(!is.na(date))
df_plot$date <- as.factor(df_plot$date)
levels(df_plot$date) <- c("June \n 2018", "July \n 2018", "August \n 2018", "September \n 2018", "October \n 2018", "November \n 2018", "December \n 2018", "January \n 2019","February \n 2019", "March \n 2019", "April \n 2019", "May \n 2019", "June \n 2019", "July \n 2019", "August \n 2019", "September \n 2019")

df_plot <- gather(df_plot, indicator, value, xrate:inflation)
df_plot$indicator <- factor(df_plot$indicator, labels = c("Growth\n ", "Inflation\n", "Unemployment\n", "Exchange\nrate"))

df_plot %>% filter(date=="March \n 2019" | date=="June \n 2019") %>%
  arrange(indicator, date)

ggplot(df_plot, aes(x=date, y=value), group=indicator) +
  geom_point(aes(col=indicator, shape=indicator), size=2) +
  geom_line(aes(group=indicator, col=indicator, linetype = indicator)) + 
  facet_grid(rows = vars(indicator), scales="free_y") +
  theme_minimal() + 
  scale_x_discrete(name = "Month") + 
  scale_y_continuous(name = "") + 
  theme(legend.position = "none") 

ggsave("figures, appendix/indicators.pdf", width = 10, height = 6, units = "in")
```

# Table 7.6
* Economic indicators and support for the AKP-MHP versus the CHP-IYI coalitions (standard errors clustered at the district level)
```{R}
# LOAD KONDA SURVEYS MERGED WITH ECON INDICATORS
load("data/df_konda_econ.RData")

#  REGRESS VOTE CHOICE ON ECONOMIC INDICATORS
df_ols <- df_merged %>% select(vote, date, ilce, istanbul, xrate, growth, unemployment, inflation)
df_ols$ilce <- as.factor(df_ols$ilce)

# AN ORDERED OUTCOME
df_ols$AKP <- ifelse(df_ols$vote!="DK", 0, NA)
df_ols$AKP <- ifelse(df_ols$vote=="AKP" | df_ols$vote=="MHP", 1, df_ols$AKP)
df_ols$AKP <- ifelse(df_ols$vote=="CHP" | df_ols$vote=="IYI", -1, df_ols$AKP)

#  SES CLUSTERED AT THE ILCE LEVEL
ols_AKP_1 <- lm_robust(AKP ~ istanbul + xrate, data=df_ols, cluster=ilce)
ols_AKP_2 <- lm_robust(AKP ~ istanbul + growth, data=df_ols, cluster=ilce)
ols_AKP_3 <- lm_robust(AKP ~ istanbul + unemployment, data=df_ols, cluster=ilce)
ols_AKP_4 <- lm_robust(AKP ~ istanbul + inflation, data=df_ols, cluster=ilce)
ols_AKP_all <- lm_robust(AKP ~ istanbul + xrate + growth + unemployment + inflation, data=df_ols, cluster=ilce)

texreg(list(ols_AKP_1, ols_AKP_2, ols_AKP_3, ols_AKP_4, ols_AKP_all), include.ci = FALSE, digits = 3, stars=c(0.01, 0.05, 0.1), custom.coef.names=c("Intercept", "Istanbul", "Exchange rate", "Growth", "Unemployment", "Inflation"))
```

# Figure 7.29
* Neighborhood-level turnout in March and June 2019 and turnout change between June and March 2019 for three measures of turnout

### Prepare the data
```{R}
rm(list = ls(all = TRUE))
results <- read.csv("data/results_neighborhood.csv")
```

## Compare turnout using multiple metrics
* Metric 1: for the simplified analysis, any vote other than for the AKP or CHP counts as abstention 
```{R}
turnout_march <- (results$AKP_march + results$CHP_march)/results$registered
turnout_june <- (results$AKP_june + results$CHP_june)/results$registered
akp_two_march <- results$AKP_march/(results$AKP_march + results$CHP_march)

df_plot <- data.frame(results$neighborhood_id,  results$neighborhood_name, akp_two_march, turnout_march, turnout_june)
df_plot <- df_plot %>% gather(month, turnout, turnout_march:turnout_june)
colnames(df_plot) <-c("neighborhood_id", "neighborhood_name", "akp_two_march", "month", "turnout")
df_plot$month <- ifelse(df_plot$month=="turnout_march", "March", "June")
df_plot$month <- factor(df_plot$month, levels = c("March", "June"))

data_plot <- df_plot %>%
  group_by(pct=cut(akp_two_march, c(0, seq(.2, .7, .1), 1)), month) %>%
  summarize(
        mean = mean(turnout, na.rm=T),
        se   =  sd(turnout, na.rm=T) / sqrt(sum(!is.na(turnout))),
        n = n()
    ) %>%
    mutate(ci_lower = ifelse(mean-1.96*se>-1, mean-1.96*se, -1)) %>%
    mutate(ci_upper = ifelse(mean+1.96*se<1, mean+1.96*se, 1))

group_labels <- c("March", "June")
ggplot(data_plot, aes(x=pct, y=mean), col=month) +
  geom_point(aes(col=month, shape=month), size=2) +
  geom_line(aes(col=month, group=month, linetype = month),  size=1) +
  scale_shape_manual(name  ="", values=c(18,16), labels=group_labels) +
  scale_linetype_manual(name  ="", values=c("solid", "dashed"), labels=group_labels) +
  scale_color_manual(name  ="", values=c("blue", "red"), labels=group_labels) +
  geom_errorbar(aes(ymin=ci_lower, ymax=ci_upper, col=month), width=0.1) +
  theme_minimal() + 
  scale_x_discrete(name = "% AKP two-party vote share in March 2019",
        labels=c("15\n(N=52)", "25\n(N=77)", "35\n(N=115)", "45\n(N=208)", "55\n(N=226)", "65\n(N=197)", "75\n(N=83)")) +
  scale_y_continuous(name = "Turnout",
        limits=c(.7, .9))

ggsave("figures, appendix/turnout_1a.pdf", width = 8, height = 6, units = "in", dpi = 600)


df_plot <- df_plot %>% arrange(neighborhood_id, month)
df_plot <- df_plot %>% group_by(neighborhood_id) %>% mutate(turnout_diff = turnout[month=="June"]-turnout[month=="March"] ) 

data_plot <- df_plot %>% filter(month=="March") %>% 
  group_by(pct=cut(akp_two_march, c(0, seq(.2, .7, .1), 1))) %>%
  summarize(
        mean = mean(turnout_diff, na.rm=T),
        se   =  sd(turnout_diff, na.rm=T) / sqrt(sum(!is.na(turnout_diff))),
        n = n()
    ) %>%
    mutate(ci_lower = ifelse(mean-1.96*se>-1, mean-1.96*se, -1)) %>%
    mutate(ci_upper = ifelse(mean+1.96*se<1, mean+1.96*se, 1))

ggplot(data_plot, aes(x=pct, y=mean,  group=1)) +
    geom_line(size=1, linetype="dashed", color="black") + 
    geom_point(size=2, shape=19, color="black") + 
  geom_errorbar(aes(ymin=ci_lower, ymax=ci_upper), width=0.1) +
  geom_hline(yintercept=0) +
  theme_minimal() + 
  scale_x_discrete(name = "% AKP two-party vote share in March 2019",
        labels=c("15\n(N=52)", "25\n(N=77)", "35\n(N=115)", "45\n(N=208)", "55\n(N=226)", "65\n(N=197)", "75\n(N=83)")) +
  scale_y_continuous(name = "Difference: June-March 2019", 
                     limits=c(-.04,.07))

ggsave("figures, appendix/turnout_1b.pdf", width = 8, height = 6, units = "in", dpi = 600)
```

* Metric 2: number of valid votes cast
```{R}
turnout_march <- (results$valid_vote_march)/results$registered
turnout_june <- (results$valid_vote_june)/results$registered
akp_two_march <- results$AKP_march/(results$AKP_march + results$CHP_march)

df_plot <- data.frame(results$neighborhood_id,  results$neighborhood_name, akp_two_march, turnout_march, turnout_june)
df_plot <- df_plot %>% gather(month, turnout, turnout_march:turnout_june)
colnames(df_plot) <-c("neighborhood_id", "neighborhood_name", "akp_two_march", "month", "turnout")
df_plot$month <- ifelse(df_plot$month=="turnout_march", "March", "June")
df_plot$month <- factor(df_plot$month, levels = c("March", "June"))

data_plot <- df_plot %>%
  group_by(pct=cut(akp_two_march, c(0, seq(.2, .7, .1), 1)), month) %>%
  summarize(
        mean = mean(turnout, na.rm=T),
        se   =  sd(turnout, na.rm=T) / sqrt(sum(!is.na(turnout))),
        n = n()
    ) %>%
    mutate(ci_lower = ifelse(mean-1.96*se>-1, mean-1.96*se, -1)) %>%
    mutate(ci_upper = ifelse(mean+1.96*se<1, mean+1.96*se, 1))

group_labels <- c("March", "June")
ggplot(data_plot, aes(x=pct, y=mean), col=month) +
  geom_point(aes(col=month, shape=month), size=2) +
  geom_line(aes(col=month, group=month, linetype = month),  size=1) +
  scale_shape_manual(name  ="", values=c(18,16), labels=group_labels) +
  scale_linetype_manual(name  ="", values=c("solid", "dashed"), labels=group_labels) +
  scale_color_manual(name  ="", values=c("blue", "red"), labels=group_labels) +
  geom_errorbar(aes(ymin=ci_lower, ymax=ci_upper, col=month), width=0.1) +
  theme_minimal() + 
  scale_x_discrete(name = "% AKP two-party vote share in March 2019",
        labels=c("15\n(N=52)", "25\n(N=77)", "35\n(N=115)", "45\n(N=208)", "55\n(N=226)", "65\n(N=197)", "75\n(N=83)")) +
  scale_y_continuous(name = "Turnout",
        limits=c(.7, .9))

ggsave("figures, appendix/turnout_2a.pdf", width = 8, height = 6, units = "in", dpi = 600)

df_plot <- df_plot %>% arrange(neighborhood_id, month)
df_plot <- df_plot %>% group_by(neighborhood_id) %>% mutate(turnout_diff = turnout[month=="June"] - turnout[month=="March"] ) 

data_plot <- df_plot %>% filter(month=="March") %>% 
  group_by(pct=cut(akp_two_march, c(0, seq(.2, .7, .1), 1))) %>%
  summarize(
        mean = mean(turnout_diff, na.rm=T),
        se   =  sd(turnout_diff, na.rm=T) / sqrt(sum(!is.na(turnout_diff))),
        n = n()
    ) %>%
    mutate(ci_lower = ifelse(mean-1.96*se>-1, mean-1.96*se, -1)) %>%
    mutate(ci_upper = ifelse(mean+1.96*se<1, mean+1.96*se, 1))

ggplot(data_plot, aes(x=pct, y=mean,  group=1)) +
  geom_line(size=1, linetype="dashed", color="black") + 
  geom_point(size=2, shape=19, color="black") + 
  geom_errorbar(aes(ymin=ci_lower, ymax=ci_upper), width=0.1) +
  geom_hline(yintercept=0) +
  theme_minimal() + 
  scale_x_discrete(name = "% AKP two-party vote share in March 2019",
        labels=c("15\n(N=52)", "25\n(N=77)", "35\n(N=115)", "45\n(N=208)", "55\n(N=226)", "65\n(N=197)", "75\n(N=83)")) +
  scale_y_continuous(name = "Difference: June-March 2019", 
                     limits=c(-.04,.07))

ggsave("figures, appendix/turnout_2b.pdf", width = 8, height = 6, units = "in", dpi = 600)
```
* Metric 3: number of voters at the polls (the government metric)
```{R}
turnout_march <- (results$total_vote_march)/results$registered
turnout_june <- (results$total_vote_june)/results$registered
akp_two_march <- results$AKP_march/(results$AKP_march + results$CHP_march)

df_plot <- data.frame(results$neighborhood_id,  results$neighborhood_name, akp_two_march, turnout_march, turnout_june)
df_plot <- df_plot %>% gather(month, turnout, turnout_march:turnout_june)
colnames(df_plot) <-c("neighborhood_id", "neighborhood_name", "akp_two_march", "month", "turnout")
df_plot$month <- ifelse(df_plot$month=="turnout_march", "March", "June")
df_plot$month <- factor(df_plot$month, levels = c("March", "June"))

data_plot <- df_plot %>%
  group_by(pct=cut(akp_two_march, c(0, seq(.2, .7, .1), 1)), month) %>%
  summarize(
        mean = mean(turnout, na.rm=T),
        se   =  sd(turnout, na.rm=T) / sqrt(sum(!is.na(turnout))),
        n = n()
    ) %>%
    mutate(ci_lower = ifelse(mean-1.96*se>-1, mean-1.96*se, -1)) %>%
    mutate(ci_upper = ifelse(mean+1.96*se<1, mean+1.96*se, 1))

group_labels <- c("March", "June")
ggplot(data_plot, aes(x=pct, y=mean), col=month) +
  geom_point(aes(col=month, shape=month), size=2) +
  geom_line(aes(col=month, group=month, linetype = month),  size=1) +
  scale_shape_manual(name  ="", values=c(18,16), labels=group_labels) +
  scale_linetype_manual(name  ="", values=c("solid", "dashed"), labels=group_labels) +
  scale_color_manual(name  ="", values=c("blue", "red"), labels=group_labels) +
  geom_errorbar(aes(ymin=ci_lower, ymax=ci_upper, col=month), width=0.1) +
  theme_minimal() + 
  scale_x_discrete(name = "% AKP two-party vote share in March 2019",
        labels=c("15\n(N=52)", "25\n(N=77)", "35\n(N=115)", "45\n(N=208)", "55\n(N=226)", "65\n(N=197)", "75\n(N=83)")) +
  scale_y_continuous(name = "Turnout",
        limits=c(.7, .9))

ggsave("figures, appendix/turnout_3a.pdf", width = 8, height = 6, units = "in", dpi = 600)

df_plot <- df_plot %>% arrange(neighborhood_id, month)
df_plot <- df_plot %>% group_by(neighborhood_id) %>% mutate(turnout_diff = turnout[month=="June"] - turnout[month=="March"] ) 

data_plot <- df_plot %>% filter(month=="March") %>% 
  group_by(pct=cut(akp_two_march, c(0, seq(.2, .7, .1), 1))) %>%
  summarize(
        mean = mean(turnout_diff, na.rm=T),
        se   =  sd(turnout_diff, na.rm=T) / sqrt(sum(!is.na(turnout_diff))),
        n = n()
    ) %>%
    mutate(ci_lower = ifelse(mean-1.96*se>-1, mean-1.96*se, -1)) %>%
    mutate(ci_upper = ifelse(mean+1.96*se<1, mean+1.96*se, 1))

ggplot(data_plot, aes(x=pct, y=mean,  group=1)) +
  geom_line(size=1, linetype="dashed", color="black") + 
  geom_point(size=2, shape=19, color="black") + 
  geom_errorbar(aes(ymin=ci_lower, ymax=ci_upper), width=0.1) +
  geom_hline(yintercept=0) +
  theme_minimal() + 
  scale_x_discrete(name = "% AKP two-party vote share in March 2019",
        labels=c("15\n(N=52)", "25\n(N=77)", "35\n(N=115)", "45\n(N=208)", "55\n(N=226)", "65\n(N=197)", "75\n(N=83)")) +
  scale_y_continuous(name = "Difference: June-March 2019", 
                     limits=c(-.04,.07))

ggsave("figures, appendix/turnout_3b.pdf", width = 8, height = 6, units = "in", dpi = 600)
```
# Figure 7.30: 
* Support for the AKP-MHP and CHP-IYI coalitions for the period January - September 2019 in Istanbul and the rest of Turkey
```{R}
rm(list = ls(all = TRUE))
load("data/df_konda.RData")

df_plot <- df_konda %>% group_by(date, istanbul) %>% 
  summarize(parties=sum(vote!="DK"),
            AKP=sum(vote=="AKP" | vote=="MHP"),
            AKP_coal=AKP/parties,
            CHP=sum(vote=="CHP" | vote=="IYI"),
            CHP_coal=CHP/parties)

df_plot <-  df_plot %>% select(date, istanbul, AKP=AKP_coal, CHP=CHP_coal)
df_plot <- gather(df_plot, party, share, AKP:CHP)
df_plot$istanbul <- recode(df_plot$istanbul, `0`="Rest of Turkey", `1`="Istanbul")
df_plot$istanbul <-  fct_rev(df_plot$istanbul)
df_plot$party <- recode(df_plot$party, AKP="AKP + MHP", CHP="CHP + IYI")

# ADD one data point for June 2019: Data available for Istanbul only
df_plot <- ungroup(df_plot) %>% add_row(date = "June \n 2019", istanbul="Istanbul", party="AKP + MHP", share=0.4451066)
df_plot <- ungroup(df_plot) %>% add_row(date = "June \n 2019", istanbul="Istanbul", party="CHP + IYI", share= 0.3977415)

df_plot$date <- ordered(df_plot$date, levels=c("June \n 2018", "July \n 2018", "September \n 2018", "October \n 2018", "November \n 2018", "January \n 2019","February \n 2019", "March \n 2019", "April \n 2019", "May \n 2019", "June \n 2019", "July \n 2019", "September \n 2019"))

ggplot(df_plot, aes(x=date, y=share), group=party) +
  geom_point(aes(col=party, shape=party), size=2) +
  geom_line(aes(group=party, col=party, linetype = party)) + 
  scale_linetype_manual(values=c("solid", "dashed")) +
  scale_color_manual(values=c("black", "red")) +
  facet_grid(cols = vars(istanbul)) +
  theme_minimal() + 
  scale_x_discrete(name = "Month") + 
  scale_y_continuous(name = "Vote share") + 
  theme(legend.title = element_blank())

ggsave("figures, appendix/diffindiff2.pdf", width = 10, height = 6, units = "in")
```
# Table 7.7
* A difference-in-differences analysis (standard errors clustered at the district level)
```{R}
rm(list = ls(all = TRUE))
load("data/df_konda.RData")

df_ols <- df_konda %>% filter(date=="May \n 2019" | date=="March \n 2019")
df_ols$date <- ifelse(df_ols$date=="May \n 2019", 1, 0)

df_ols$AKP <- ifelse(df_ols$vote!="DK", 0, NA)
df_ols$AKP <- ifelse(df_ols$vote=="AKP" | df_ols$vote=="MHP", 1, df_ols$AKP)

df_ols$CHP <- ifelse(df_ols$vote!="DK", 0, NA)
df_ols$CHP <- ifelse(df_ols$vote=="CHP" | df_ols$vote=="IYI", 1, df_ols$CHP)

ols_1 <- lm_robust(AKP ~ date + istanbul + date:istanbul, data=df_ols, cluster=ilce)
ols_2 <- lm_robust(CHP ~ date + istanbul + date:istanbul, data=df_ols, cluster=ilce)

texreg(list(ols_1, ols_2), include.ci = FALSE, digits = 3, custom.model.names = c("AKP-MHP", "CHP-IYI"), stars=c(0.01, 0.05, 0.1))
```